package com.livefornowstudios.demos.helloworld.client.uibinder;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.livefornowstudios.demos.helloworld.client.GreetingService;
import com.livefornowstudios.demos.helloworld.client.GreetingServiceAsync;

public class LandingPage extends Composite {

	private static LandingPageUiBinder uiBinder = GWT
			.create(LandingPageUiBinder.class);

	interface LandingPageUiBinder extends
			UiBinder<Widget, LandingPage> {
	}
	
	@UiField Button sendButton;
	@UiField PopupPanel dialogBox;
	@UiField TextBox nameField;
	@UiField Label textToServerLabel;
	@UiField Label serverResponseLabel;
	@UiField Button closeButton;
	
	
	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);

	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";


	public LandingPage(String firstName) {
		initWidget(uiBinder.createAndBindUi(this));
		sendButton.setText(firstName);
		dialogBox.setVisible(false);
	}

	@UiHandler("sendButton")
	void sendButton_onClick(ClickEvent e) {
		sendNameToServer();
	}
	
	@UiHandler("closeButton")
	void closeButton_onClick(ClickEvent e) {
		dialogBox.hide();
		sendButton.setEnabled(true);
		sendButton.setFocus(true);
	}
	
	/**
	 * Send the name from the nameField to the server and wait for a response.
	 */
	private void sendNameToServer() {
		sendButton.setEnabled(false);
		String textToServer = nameField.getText();
		textToServerLabel.setText(textToServer);
		serverResponseLabel.setText("");
		greetingService.greetServer(textToServer,
				new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						// Show the RPC error message to the user
						dialogBox.setTitle("Remote Procedure Call - Failure");
						serverResponseLabel.addStyleName("serverResponseLabelError");
						serverResponseLabel.setText(SERVER_ERROR);
						dialogBox.center();
						closeButton.setFocus(true);
					}

					public void onSuccess(String result) {
						dialogBox.setTitle("Remote Procedure Call");
						serverResponseLabel.removeStyleName("serverResponseLabelError");
						serverResponseLabel.setText(result);
						dialogBox.center();
						closeButton.setFocus(true);
					}
				});
	}
	
	
	

}
